package com.isdust.zor;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.isdust.zor.core.R;
import com.isdust.zor.core.util.ConfigHelper;
import com.isdust.zor.core.util.SerializableList;
import com.isdust.zor.libs.DPCManager;
import com.isdust.zor.libs.DPDecor;
import com.isdust.zor.libs.DPMode;
import com.isdust.zor.libs.DatePicker;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity implements DatePicker.OnClickSignIn {

    DatePicker myDatepicker;
    private Context mContext;

    private String[] strings = new String[3];
    private String today;

    List<String> tmp = new SerializableList<>();
    private DPCManager dpcManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDatepicker = (DatePicker) findViewById(R.id.my_datepicker);
        mContext = this;
        initData();
        init();

    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
    }

    private void initData() {
        tmp = (SerializableList<String>) ConfigHelper.readObject(mContext, "signin");
        //自定义背景绘制示例
        if (tmp != null)
            return;
        tmp = new SerializableList<>();
        tmp.add("2016-10-3"); //yyyy-M-d
        tmp.add("2016-10-1");
        tmp.add("2016-10-9");
        tmp.add("2016-10-10");
        tmp.add("2016-10-11");
        tmp.add("2016-10-12");
    }

    private String getToday() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-d");
        return simpleDateFormat.format(date);
    }

    private void init() {
        dpcManager = DPCManager.getInstance();
        dpcManager.clearnDATE_CACHE(); //清除cache

        dpcManager.setDecorBG(tmp); //预先设置日期背景 一定要在在开始设置

        today = getToday();
        if (tmp.contains(today))
            myDatepicker.setRightTitle(true);
        strings = today.split("-");

        myDatepicker.setDate(Integer.parseInt(strings[0]), Integer.parseInt(strings[1])); //设置日期

        myDatepicker.setMode(DPMode.NONE); //设置选择模式

        myDatepicker.setFestivalDisplay(false); //是否显示节日
        myDatepicker.setTodayDisplay(false); //是否高亮显示今天
        myDatepicker.setHolidayDisplay(false); //是否显示假期
        myDatepicker.setDeferredDisplay(false); //是否显示补休
        myDatepicker.setIsScroll(false); //是否允许滑动 false表示左右上下都不能滑动  单项设置上下or左右 你需要自己扩展
        myDatepicker.setIsSelChangeColor(true, getResources().getColor(R.color.font_white_one)); //设置选择的日期字体颜色,不然有的背景颜色和默认的字体颜色不搭

        myDatepicker.setLeftTitle(strings[1] + "月"); //左上方text
        myDatepicker.setOnClickSignIn(this); //点击签到事件

        //设置预先选中日期的背景颜色
        myDatepicker.setDPDecor(new DPDecor() {
            @Override
            public void drawDecorBG(Canvas canvas, Rect rect, Paint paint) {
                paint.setColor(getResources().getColor(R.color.blue));
                canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 4F, paint);
            }
        });

    }

    @Override
    public void signIn() {
        ConfigHelper.saveObject(mContext, "test", strings);
        if (myDatepicker.getIsSignIn()) {
            return;
        }
        //动态更新的时候必须  清除cache
        dpcManager.clearnDATE_CACHE(); //清除cache
        //重新设置日期
        tmp.add(today);
        dpcManager.setDecorBG(tmp);

        myDatepicker.setDate(Integer.parseInt(strings[0]), Integer.parseInt(strings[1])); //设置日期
        myDatepicker.setLeftTitle(strings[1] + "月");
        myDatepicker.setRightTitle(true);

        ConfigHelper.saveObject(mContext, "signin", tmp);
        myDatepicker.setDPDecor(new DPDecor() {
            @Override
            public void drawDecorBG(Canvas canvas, Rect rect, Paint paint) {
                paint.setColor(getResources().getColor(R.color.blue));
                canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 4F, paint);
            }
        });
        myDatepicker.invalidate(); //刷新
    }
}
